package com.sky.jdkurlclient;


import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.nio.charset.StandardCharsets;

public class JdkUrlClient {


    /**
     * JDK原生发送http请求的API
     */
    @Test
    public void jdkUrlClient() throws IOException {

        //请求路径
        String urls = "https://www.baidu.com/";

        URL url = new URL(urls);
        URLConnection openConnection = url.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection) openConnection;

        try (InputStream inputStream = httpURLConnection.getInputStream();
             InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(inputStreamReader)
        ){

            String line;
            while( (line = br.readLine()) != null ){
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }





}
